package cn.amoqi.springbootredis2.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @author: zhuiFeng
 * @date: 2021年06月16日 18:40
 */
@RestController
@Slf4j
public class IndexController {
    private StringRedisTemplate redisTemplate;
    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @RequestMapping("/")
    public void redisTest(){
        //key - value
        redisTemplate.opsForValue().set("name","lemon");
        String name = redisTemplate.opsForValue().get("name");
        log.info("name is:{}",name);

        //hash
        redisTemplate.opsForHash().put("school","shandong","山大");
        redisTemplate.opsForHash().put("school","shanghai","交大");
        redisTemplate.opsForHash().put("school","jinan","济南大学");
        redisTemplate.opsForHash().put("school","qingdao","青岛大学");
        Map<Object, Object> school = redisTemplate.opsForHash().entries("school");
        for (Map.Entry<Object, Object> objectObjectEntry : school.entrySet()) {
            Object key = objectObjectEntry.getKey();
            Object value = objectObjectEntry.getValue();
            log.info("key is:{},value is:{}",key,value);
        }
        Object value = redisTemplate.opsForHash().get("school", "qingdao");
        log.info("qingdao school is :{}",value);

        //set
        redisTemplate.opsForSet().add("xuesheng","zhangsan","lisi","wangwu","wangwu","wangwu2");
        Set<String> xuesheng = redisTemplate.opsForSet().members("xuesheng");
        xuesheng.stream().forEach(e->log.info("学生列表：{}",e));

        //zset
        redisTemplate.opsForZSet().add("xuesheng2","zhangsan",60);
        redisTemplate.opsForZSet().add("xuesheng2","lisi",80);
        redisTemplate.opsForZSet().add("xuesheng2","wangwu",90);
        redisTemplate.opsForZSet().range("xuesheng2",0,-1).stream().forEach(e->log.info("xuesheng2:{}",e));

        //list
        redisTemplate.opsForList().leftPush("list","sss");
        redisTemplate.opsForList().leftPush("list","sss2");
        redisTemplate.opsForList().leftPush("list","sss3");
        redisTemplate.opsForList().leftPush("list","sss0");
        redisTemplate.opsForList().range("list",0,-1).stream().forEach(log::info);
    }
}
